package com.anti_poverty_detection_system.springboot.controller;

import com.anti_poverty_detection_system.springboot.mapper.RegionUpperMapper;
import com.anti_poverty_detection_system.springboot.model.Regions;
import com.anti_poverty_detection_system.springboot.service.IRegionsService;
import com.anti_poverty_detection_system.springboot.service.dto.CascaderOption;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZITONG BAI
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/regions")
public class RegionsController {
    @Autowired
    IRegionsService regionsService;
    @Autowired
    RegionUpperMapper regionUpperMapper;
    @GetMapping("get_regions")
    public ResponseEntity<Page<Regions>> getRegions(
            @RequestParam int page,
            @RequestParam int size,
            @RequestParam(required = false) String keywords
    ) {

        Page<Regions> regionsPage = new Page<>(page, size);
        if (keywords == null) {
            Page<Regions> regions = regionsService.getRegions(regionsPage);
            return ResponseEntity.ok().body(regions);
        } else {
            Page<Regions> searchRegions = regionsService.getSearchRegions(regionsPage, keywords);
            return ResponseEntity.ok().body(searchRegions);
        }
    }

    @PostMapping("add_regions")
    public ResponseEntity<String> addRegions(@RequestBody Regions regions) {
        // 先拼接 regionName
        regions.setRegionName(regions.getProvince() +
                regions.getCity() +
                regions.getCounty() +
                regions.getTownship() +
                regions.getVillage());

        // 查重
        Regions regions_test = regionsService.getRegionsByRegionName(regions.getRegionName());
        if (regions_test != null &&
                Objects.equals(regions_test.getRegionName(), regions.getRegionName()) &&
                Objects.equals(regions_test.getProvince(), regions.getProvince()) &&
                Objects.equals(regions_test.getCity(), regions.getCity()) &&
                Objects.equals(regions_test.getCounty(), regions.getCounty()) &&
                Objects.equals(regions_test.getTownship(), regions.getTownship()) &&
                Objects.equals(regions_test.getVillage(), regions.getVillage())) {
            return ResponseEntity.badRequest().body("行政区已存在");
        } else {
            // 保存到数据库
            regionsService.save(regions);
            regionsService.addRegionUpper(regions);
            return ResponseEntity.ok().body("行政区添加成功");
        }
    }


    // 更新行政区逻辑
    @PostMapping("update_regions")
    public ResponseEntity<String> updateRegions(@RequestBody Regions regions) {
        Regions regions_real = regionsService.getRegionsByRegionName(regions.getRegionName());
        if (regions_real == null) {
            return ResponseEntity.badRequest().body("该行政区不存在");
        } else {
            regions_real.setRegionNumber(regions.getRegionNumber());
            regions_real.setProvince(regions.getProvince());
            regions_real.setCity(regions.getCity());
            regions_real.setCounty(regions.getCounty());
            regions_real.setTownship(regions.getTownship());
            regions_real.setVillage(regions.getVillage());
            regions_real.setRegionName(regions.getProvince() +
                    regions.getCity() +
                    regions.getCounty() +
                    regions.getTownship() +
                    regions.getVillage());
            regionsService.saveOrUpdate(regions_real);

            return ResponseEntity.ok().body("更新行政区完成");
        }
    }

    @PostMapping("delete_region")
    public ResponseEntity<String> deleteRegion(@RequestBody Regions regions) {
        Regions regions1=regionsService.getRegionsByRegionName(regions.getRegionName());
        boolean isDeleted=regionsService.deleteRegionByRegionName(regions1.getRegionName());
        if (isDeleted) {
            return ResponseEntity.ok("行政区删除成功");
        } else {
            return ResponseEntity.badRequest().body("行政区删除失败");
        }
    }

    @PostMapping("get_county")
    public ResponseEntity<String> getCounty(@RequestBody Regions regions){
        Regions regions1 = regionsService.getRegionsByRegionName(regions.getRegionName());
        return ResponseEntity.ok().body(regions1.getCounty());
    }

    @PostMapping("get_villages")
    public ResponseEntity<List<CascaderOption>> getVillages(@RequestBody Regions regions) {
        String county = regions.getCounty();
        List<CascaderOption> townshipOptions = new ArrayList<>();
        List<String> townships = regionUpperMapper.findTownshipsByCounty(county);
        for (String township : townships) {
            CascaderOption townshipOption = new CascaderOption(township,township);
            List<String> villages = regionUpperMapper.findVillagesByTownShip(township);
            List<CascaderOption> villageOptions = new ArrayList<>();
            for (String village : villages) {
                villageOptions.add(new CascaderOption(village,village));
            }
            townshipOption.setChildren(villageOptions);
            townshipOptions.add(townshipOption);
        }
        return ResponseEntity.ok().body(townshipOptions);
    }

}
